﻿using Soft.Xml;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace Soft.Service
{

    /// <summary>
    /// 服务端LocalJobConfig配置文件
    /// </summary>
    public sealed class ServiceConfig : BaseSubElement, IEnumerable<DllConfig>, IEnumerable
    {

        public ServiceConfig(XmlElement current) : base(current)
        {
        }


        /// <summary>
        /// DllConfig的节点数
        /// </summary>
        public int DllsCount
        {
            get
            {
                return base.CurrentElement.SelectNodes(typeof(DllConfig).Name).Count;
            }
        }


        /// <summary>
        /// 获取DllConfig
        /// </summary>
        /// <param name="index"></param>
        /// <returns></returns>
        public DllConfig this[int index]
        {
            get
            {
                return new DllConfig((XmlElement)base.CurrentElement.SelectNodes(typeof(DllConfig).Name)[index]);
            }
        }


        /// <summary>
        /// 创建dllConfig
        /// </summary>
        /// <returns></returns>
        public DllConfig CreateDllConfig()
        {
            XmlElement xmlElement = base.CurrentElement.OwnerDocument.CreateElement(typeof(DllConfig).Name);
            base.CurrentElement.AppendChild(xmlElement);
            return new DllConfig(xmlElement);
        }


        /// <summary>
        /// 清空
        /// </summary>
        public void Clear()
        {
            base.CurrentElement.InnerXml = "";
        }


        /// <summary>
        /// 移除dllConfig
        /// </summary>
        /// <param name="item"></param>
        public void Remove(DllConfig item)
        {
            base.CurrentElement.RemoveChild(item.CurrentElement);
        }


        /// <summary>
        /// 获取dllConfig
        /// </summary>
        /// <returns></returns>
        public IEnumerator<DllConfig> GetEnumerator()
        {
            foreach (object obj in base.CurrentElement.SelectNodes(typeof(DllConfig).Name))
            {
                XmlNode xmlNode = (XmlNode)obj;
                yield return new DllConfig((XmlElement)xmlNode);
            }
        }


        /// <summary>
        /// 获取枚举器
        /// </summary>
        /// <returns></returns>
        IEnumerator IEnumerable.GetEnumerator()
        {
            return this.GetEnumerator();
        }


    }
}
